<template>
  <div class="page-modal">
    <el-dialog v-model="centerDialogVisible" title="新建" width="30%" center>
      <DlForm v-bind="modalConfig" :formData="formData"></DlForm>
      <slot></slot>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleConfirmClick">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, watch } from "vue";
import DlForm from "@/base-ui/form";
import { useStore } from "vuex";
export default defineComponent({
  components: {
    DlForm,
  },
  props: {
    pageName: {
      type: String,
      required: true,
    },
    otherInfo: {
      type: Object,
      default: () => ({}),
    },
    modalConfig: {
      type: Object,
      required: true,
    },
    defaultInfo: {
      type: Object,
      default: () => ({}),
    },
  },
  setup(props, { emit }) {
    const centerDialogVisible = ref(false);
    const formData = ref<any>({});
    const store = useStore();
    watch(
      () => props.defaultInfo,
      (newValue) => {
        console.log("newValue", newValue);
        for (const item of props.modalConfig.formItems) {
          formData.value[`${item.field}`] = newValue[`${item.field}`];
        }
      }
    );
    //点击确定按钮的逻辑
    const handleConfirmClick = () => {
      centerDialogVisible.value = false;
      if (Object.keys(props.defaultInfo).length) {
        //编辑
        store.dispatch("system/editPageDataAction", {
          pageName: props.pageName,
          editData: { ...formData.value, ...props.otherInfo },
          id: props.defaultInfo.id,
        });
        console.log("id", props.defaultInfo.id);
      } else {
        //新建
        store.dispatch("system/createPageDataAction", {
          pageName: props.pageName,
          newData: { ...formData.value, ...props.otherInfo },
        });
      }
    };
    return {
      centerDialogVisible,
      formData,
      handleConfirmClick,
    };
  },
});
</script>

<style scoped></style>
